#include "DL_Service.h"
#include "SM.h"
#include "DL_MH.h"
#include "DL_CM.h"
#include "DL_EH.h"
#include "AL_ISDU.h"
#include "PL.h"
#include "Data_Buffer.h"
void Event_DL_Mode_ind(void){

}

void Service_DL_Mode_ind(MODE_TYPE Mode){
    Event_DL_Mode_ind();
    SM_Mode=Mode;
    
}

void Service_DL_Control_ind(PDOUT_VALUE_TYPE PDOUT_VALUE_Type){

}

void Service_DL_Control_req(PDIN_VALUE_TYPE PDIN_VALUE_Type){
    if(CM_STATE!=CM_Idle_1){
        return;
    }
    if(PDIN_VALUE_Type==PDININVALID){
           *MC_STRUCT.CKS=(*MC_STRUCT.CKS)|0x40;//第6位置为1
    } else{
          *MC_STRUCT.CKS= (*MC_STRUCT.CKS)&0xBF; //第6位之为0
    }
   
} 
//替代EH中的DL_Event作用
BOOL Service_DL_Event(Event_TYPE Type,Event_MODE Mode, uint8_t EventCode){
    if(EH_STATE!=EH_Idle_1){
         return False;
    }
    uint8_t i;
    for( i=0;i<6;i++){
        if((EventMemory.EventMemoryStruct.StatusCode&(1<<i))!=1<<i){ //说明此EventEntry没有占用
            EventMemory.EventMemoryStruct.StatusCode=EventMemory.EventMemoryStruct.StatusCode|1<<i; //相应位置位
            break;
        }
    }
    
    EventMemory.EventMemoryStruct.EventEntrys[i].EventCode=EventCode;
    EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer=0x04;
    switch ( Type)
    {
    case NOTIFICATION  :
			 
         EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer=EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer|0x10;  
        break;
    case WARNING  :
        EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer=EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer|0x20;  
        break;
    case Event_ERROR  :
        EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer=EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer|0x30;  
        break;
    default:
       EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer=EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer|0x30; 
        break;
    }

    switch (Mode)
    {
    case SINGLESHOT:
       EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer=EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer|0x40; 
        break;
    case DISAPPEARS:
       EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer=EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer|0x80; 
        break;
    case APPEARS:
       EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer=EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer|0xC0; 
        break;
    default:
        EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer=EventMemory.EventMemoryStruct.EventEntrys[i].EventQualifer|0xC0; 
        break;
    }
    return True;

}

void Service_DL_EventTrigger(void){
    if(EH_STATE==EH_Idle_1){
        EH_STATE=EH_FreezeEventMemory_2;
        *MC_STRUCT.CKS=*MC_STRUCT.CKS|0x80; 
    }
}

//Page写的反应动作
void Service_AL_Write_rsp(void){

}


void Service_DL_ISDUTransport_ind(void){
	
	Event_DL_ISDUTransport_ind();
}


void Service_DL_ISDUAbort(void){
	
	//待更新
}

void Service_DL_PDOutputTransport_ind(void){
	//待更新
	PL_Output(PDDataOut,MC_STRUCT.PDout_Len); //PDout输出
}

void Service_DL_PDCycle_ind(void){
	
	
}

